<?php
/**
 * Project:     Rebel PHP Framework
 * File:        Db.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.rebelphpframework.org
 * @copyright 2007 Emanuele Colonnelli
 * @author Emanuele Colonnelli <emanuele.colonnelli at gmail dot com>
 * @package kernel
 * @version 1.0
 */

if(define(__CLASS__,true)) {
	
	
	class Rebel_DB_PDO_Db {

		private static $instance;
		/**
		 * Rebel_Config_Rebel_Config_XMLConfig object
		 *
		 * @var Rebel_Config_XMLConfig
		 */
		private static $xcfg;
		/**
		 * PDO Connection handler
		 *
		 * @var PDO
		 */
		private static $dbh;
		
		public function __construct() {
			
			self::$xcfg = Rebel_Config_XMLConfig::getInstance();
			
			self::$dbh = new PDO(self::$xcfg->{"database.dsn"},self::$xcfg->{"database.username"},self::$xcfg->{"database.password"});
			self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			self::$instance = $this;
		}//:-
		
		/**
		 * @return PDO
		 */
		public static function getInstance() {
				return self::$instance ? self::$instance : new self;
		}//:-
		
		public function __call($func,$args) {
			
			return call_user_func_array(array(self::$dbh,$func),$args);
			
		}
		
		
		public static function  validateQuery(PDOStatement &$stmt) {
			
			if(!$stmt->execute()) {
				$err = $stmt->errorInfo();
				throw new Exception($err[2],$err[1]);
			}
			
		}//:-
		
		
		/**
		 * WORK IN PROGESS
		 * 
		 * Call execute Method of PDOStatement, accepts a hash with :param_name => value and 
		 * a fetchType param: 
		 * null => no Fetch ( use this for insert update delete ) returns a boolean
		 * object => returns an array of record objects
		 * all => returns all records as associative array
		 *
		 * @param String $query
		 * @param Array $boundParams
		 * @param Mixed $fetchMode
		 */
		public function improvedExecute($query,$boundParams=array(),$fetchMode=null) {
			
			$stmt = self::$instance->prepare($query);
			if(0 < count($boundParams)) {
				foreach ($boundParams as $v) {
					$stmt->bindParam(':$k',$v);
				}
			}
			if(!$stmt->execute()) {
				$err = $stmt->errorInfo();
				throw new Exception($err[2],$err[1]);
			}
			
			return $stmt->fetchAll($fetchMode ? $fetchMode : PDO::FETCH_ASSOC);
		}//:-
		
	}

}